package com.ruitu.mall_98_2.mvp.presenter;

import com.beanu.arad.utils.Logg;
import com.beanu.arad.utils.ToastUtils;
import com.ruitu.mall_98_2.mvp.contract.ByNowFillInOrderContract;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.mine.Coupon;
import com.ruitu.router_module.bean.order.CommitOrder;
import com.ruitu.router_module.bean.order.ConfirOrder;

import java.util.List;

public class ByNowFillInOrderPresenter extends ByNowFillInOrderContract.Presenter {
    @Override
    public void buyNowForSettlement(String accountId, String goodsId, String num, String skuId, String status, String addressId) {
        mModel.buyNowForSettlement(accountId, goodsId, num, skuId, status, addressId)
                .subscribe(new SubscriberImpl<BaseResult<CommitOrder>>() {
                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                        Logg.i(e.toString());
                        mView.setFailed();
                        ToastUtils.showShort("加载失败");
                    }

                    @Override
                    public void onNext(BaseResult<CommitOrder> commitOrderBaseResult) {
                        mView.hideProgress();
                        if (commitOrderBaseResult.isSuccess()) {
                            mView.setSuc(commitOrderBaseResult.getData());
                        } else {
                            ToastUtils.showShort(commitOrderBaseResult.getMsg());
                        }
                    }
                });
    }

    @Override
    public void buyNowForApp(String accountId, String goodsId, String num, String skuId, String status, String addressId, String couponId) {
        mModel.buyNowForApp(accountId, goodsId, num, skuId, status, addressId, couponId)
                .subscribe(new SubscriberImpl<BaseResult<ConfirOrder>>() {
                    @Override
                    public void onError(Throwable e) {
                        Logg.i(e.toString());
                        mView.hideProgress();
                        ToastUtils.showShort("提交订单失败");
                    }

                    @Override
                    public void onNext(BaseResult<ConfirOrder> confirOrderBaseResult) {
                        mView.hideProgress();
                        if (confirOrderBaseResult.isSuccess()) {
                            mView.setPaySuc(confirOrderBaseResult.getData());
                        } else {
                            ToastUtils.showShort(confirOrderBaseResult.getMsg());
                        }
                    }
                });
    }

    @Override
    public void selectUseCoupons(String accountId, String userCouponIds, String goodId, String totalMoney, String companyId) {
        mModel.selectUseCoupons(accountId, userCouponIds, goodId, totalMoney, companyId)
                .subscribe(new SubscriberImpl<BaseResult<List<Coupon>>>() {
                    @Override
                    public void onError(Throwable e) {
                        mView.hideProgress();
                        ToastUtils.showShort("获取优惠券失败");
                    }

                    @Override
                    public void onNext(BaseResult<List<Coupon>> listBaseResult) {
                        mView.hideProgress();
                        if (listBaseResult.isSuccess()) {
                            mView.setListCoupon(listBaseResult.getData());
                        } else {
                            ToastUtils.showShort(listBaseResult.getMsg());
                        }
                    }
                });
    }
}
